<?php

class Db_Tables2_BDictProduct
{
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function GetProducts($status=1)
	{
		$result = array();
		
		$select = $this->dealers_db->select();
		$select->from("b_dict_product", array("product_id", "product_name"));
		
		if($status)
		{
			$select->where("status = ?", 1);
		}
		
		$data = $this->dealers_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				$result[$d['product_id']] = $d['product_name'];
			}
		}
		
		return $result;
	}
	
	function FetchProductDetails($product_id)
	{
		$result = array();
		
		if($product_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_dict_product", "*");
			$select->where("product_id = ?", $product_id);
			$result = $this->dealers_db->fetchRow($select);
		}
		
		return $result;
	}
	
	function GenerateOrderSid($product_id)
	{
		$result = "";
		
		if($product_id)
		{
			$select = $this->dealers_db->select();
			$select->from("b_dict_product", array("od_prex", "current_ct"));
			$select->where("product_id = ?", $product_id);
			$row = $this->dealers_db->fetchRow($select);
			
			$prex = NULL;
			$ct = 0;
			
			if($row)
			{
				if($row['od_prex'])
				{
					$prex = $row['od_prex'];
				}
				
				if($row['current_ct'])
				{
					$ct = $row['current_ct'];
				}else{
					$ct = 0;
				}
			}
			
			if($prex && is_numeric($ct))
			{
				$result .= $prex;
				$result .= date("y");
				$result .= date("m");
				
				$ct += 1;
				$result .= sprintf("%04d", $ct);
				
				$set = array("current_ct" => $ct);
				$where = "product_id = ".$product_id;
				$this->dealers_db->update("b_dict_product", $set, $where);
			}
		}
		
		return $result;
	}
}